<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class model_elibrary extends CI_Model{
    var $table      = "tbl_elibrary";
    var $primary    = "id_elibrary";
    
    public function __construct()
    {
        parent::__construct();
    }
    
    function get_elibrary($kategory, $limit=0, $offset=0){
        $q = $this->db->query("SELECT * FROM " . $this->table . " WHERE kategori = '".$kategory."'  ORDER BY `datemodified` DESC LIMIT ".$limit."");
        return $q->result();
    }
    function get_all(){
        $q = $this->db->query("SELECT * FROM `".$this->table."`");
        return $q->result();
    }
    function save($isi){
        if($this->db->insert($this->table,$isi)) return true;
        else return false;
    }
    function update($id,$array){
        $this->db->where($this->primary,$id);	
        if($this->db->update($this->table,$array)) return TRUE;
        else return FALSE;
    }
    function delete($id){
        if($this->db->delete($this->table, array($this->primary => $id))) return true;
        else return false; 
    }
    function select_by_id($id, $mod=''){
        $q = $this->db->query("SELECT * FROM `".$this->table."` WHERE `".$this->primary."` = '".$id."'");
        if(strtolower($mod) == "row") return $q->row();
        return $q->result();
    }
    function get_all_elibrary_data($limit=0, $offset=0, $conditions=''){
        if( !empty($conditions) ){
            $conditions = str_replace("%judul%",    "judul",    $conditions);
            $conditions = str_replace("%kategori%", "kategori", $conditions);
        }
        
        $sql  = 'SELECT SQL_CALC_FOUND_ROWS * FROM ' . $this->table . ' ';
        if( !empty($conditions) ) $sql .= 'WHERE ' . $conditions . ' ';
        $sql .= 'ORDER BY datecreated DESC';
        
        if( $limit ) $sql .= ' LIMIT ' . $offset . ', ' . $limit;
        
        $query = $this->db->query($sql);
        if(!$query || !$query->num_rows()) return false;
        
        return $query->result();
    }
}
/* End of file model_elibrary.php */
/* Location: ./application/models/model_elibrary.php */
